home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / ax25rout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-27  |  58.1 KB  |  2,195 lines

  1. /* Low level AX.25 frame processing - address header and routing */
  2. /*                                  */
  3. /* 9205xx DAMA by DL1BKE                       */
  4. /* 9409xx DAMA bugfixes, T3 handling, T6 timeout, flushed state     */
  5. /*       by DG1ZX/DL6ZBA                     */
  6. /* 940920 fixed bad pointers in ax_recv() axheard traffic fixed  */
  7. /*        by DC0HK                                               */
  8. /* 940925 fixed problem with ip.tos=16 (low_delay) forceing DA   */
  9. /*        mode when we are active in a VC mode connection        */
  10. /*        by DC0HK                                               */
  11. /* 940930 fixed problem with flexnet search                      */
  12. /*        fixed T4 timer digi busy sent RNR wnos now does not    */
  13. /*        poll after T4 expire                                   */
  14. /*        will never send any UI-frames if there is a dama_slave */
  15. /*        connection exist on the interface, wait for new info   */
  16. /*        from dama developers                                   */
  17. /*        by DG1ZX/DL6ZBA                                        */
  18. /* 941019 added retries in dama_flush and fixes problems with    */
  19. /*       multiples received DISCs.                  */
  20. /*       Fixed bug when using WNOS as digipeater with dama link */
  21. /*       at one side and non dama on the other port          */
  22. /*        by DG1ZX/DL6ZBA                                        */
  23. /*                                  */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include "global.h"
  28. #include "config.h"
  29. #include "socket.h"
  30. #include "mbuf.h"
  31. #include "iface.h"
  32. #include "timer.h"
  33. #include "arp.h"
  34. #include "slip.h"
  35. #include "ax25.h"
  36. #ifdef NETROM
  37. #include "netrom.h"
  38. #endif
  39. #include "ip.h"
  40. #include "tcp.h"
  41. #include "trace.h"
  42. #include "files.h"
  43. #include "icmp.h"
  44.  
  45. #define axptr(a)        ((struct ax25_addr *) (a))
  46. #define next_seq(n)     (((n) + 1) & MMASK)
  47.  
  48. char Mycall[AXALEN]     = "\0";
  49. char Nomycall[]     = "Mycall not set\n";
  50.  
  51. #ifdef NETROM
  52. extern void nr_derate __ARGS((struct ax25_cb *axp));
  53. #endif
  54.  
  55. struct ax25_cb *Ax25_cb = NULLAX25;
  56. struct iface *axroute_default_ifp = NULLIF;
  57. struct axroute_tab *axroute_tab[AXROUTESIZE];
  58.  
  59. static struct ax25_cb * near cr_ax25 __ARGS((char *remote,char *local,struct iface *iface));
  60.  
  61. static int near axproto_recv __ARGS((struct iface *ifp,struct mbuf *bp,char repeated));
  62. static int near axroute __ARGS((struct ax25_cb *cp,struct mbuf *bp));
  63. static int near hash_function __ARGS((char  *call,int rel));
  64. static int near space_ax25 __ARGS((struct ax25_cb *cp));
  65. static int near put_reseq __ARGS((struct ax25_cb *cp,struct mbuf *bp,int  ns));
  66. static int near is_flexnet __ARGS((char *call,int store));
  67. static int near busy __ARGS((struct ax25_cb *cp));
  68. static void near init_timer __ARGS((struct ax25_cb *axp));
  69. static void near setaxstate __ARGS((struct ax25_cb *axp,int newstate));
  70. static void near send_packet __ARGS((struct ax25_cb *cp,int type,int cmdrsp,struct mbuf *data));
  71. static void near send_ack __ARGS((struct ax25_cb *cp,int  cmdrsp));
  72. static void near try_send __ARGS((struct ax25_cb *cp,int fill_sndq,int mode));
  73. static void near inc_t1 __ARGS((struct ax25_cb *cp));
  74. static void near reset_t1 __ARGS((struct ax25_cb *cp));
  75. static void near dama_on __ARGS((struct iface *ifp));
  76. static void near dama_off __ARGS((struct iface *ifp));
  77. static void near dama_flush __ARGS((struct ax25_cb *cp));
  78.  
  79. /* Default AX.25 parameters */
  80. int16 T1init =         10;        /* Retransmission timeout */
  81. int16 T2init =         2;        /* Acknowledgement delay timeout */
  82. int16 T3init =        600;        /* keep-alive polling */
  83. int16 T4init =         60;        /* Busy timeout */
  84. int16 T5init =         1;        /* Packet assembly timeout */
  85. int16 T6init =         300;        /* DAMA timeout */
  86. int16 Maxframe =     2;        /* Stop and wait */
  87. int16 Retries =     10;        /* 10 retries */
  88. int16 Axwindow =     2048;        /* 2K incoming text before RNR'ing */
  89. int16 Paclen =         256;        /* 256-byte I fields */
  90. int16 Pthresh =     64;        /* Send polls for packets larger than this */
  91. int16 Digipeat =     2;        /* Controls digipeating */
  92. int   T3disc =         1;        /* 0 = polling, 1 = disconnecting */
  93.  
  94. /* List of AX.25 multicast addresses in network format (shifted ascii).
  95.  * Only the first entry is used for transmission, but an incoming
  96.  * packet with any one of these destination addresses is recognized
  97.  * as a multicast.
  98.  */
  99. char Ax25multi[][AXALEN] = {
  100.     'Q'<<1, 'S'<<1, 'T'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1,    /* QST */
  101.     'N'<<1, 'O'<<1, 'D'<<1, 'E'<<1, 'S'<<1, ' '<<1, '0'<<1,    /* NODES */
  102.     'I'<<1, 'D'<<1, ' '<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1,    /* ID */
  103. /*    'M'<<1, 'A'<<1, 'I'<<1, 'L'<<1, ' '<<1, ' '<<1, '0'<<1,    /* MAIL */
  104. /*    'O'<<1, 'P'<<1, 'E'<<1, 'N'<<1, ' '<<1, ' '<<1, '0'<<1,    /* OPEN */
  105. /*    'C'<<1, 'Q'<<1, ' '<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1,    /* CQ */
  106. /*    'B'<<1, 'E'<<1, 'A'<<1, 'C'<<1, 'O'<<1, 'N'<<1, '0'<<1,    /* BEACON */
  107.     'R'<<1, 'M'<<1, 'N'<<1, 'C'<<1, ' '<<1, ' '<<1, '0'<<1,    /* RMNC */
  108. /*    'A'<<1, 'L'<<1, 'L'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1,    /* ALL */
  109.     'F'<<1, 'L'<<1, 'X'<<1, 'N'<<1, 'E'<<1, 'T'<<1, '0'<<1,    /* FLXNET */
  110.     '\0',
  111. };
  112.  
  113. /* New-style frame segmenter. Returns queue of segmented fragments, or
  114.  * original packet if small enough
  115.  *
  116.  * struct mbuf *bp;     Complete packet
  117.  * int16 ssize;         Max size of frame segments
  118.  */
  119.  
  120. #ifdef NETROM
  121. struct mbuf *
  122. #else
  123. static struct mbuf *
  124. #endif
  125. segmenter(struct mbuf *bp,int16 ssize) {
  126.   struct mbuf *bp1, *bptmp, *result = NULLBUF;
  127.   int16 len, offset = 0;
  128.   int segments;
  129.  
  130.   /* See if packet is too small to segment. Note 1-byte grace factor
  131.    * so the PID will not cause segmentation of a 256-byte IP datagram.
  132.    */
  133.   if((len = len_p(bp)) <= ssize + 1)
  134.     return bp;    /* Too small to segment */
  135.  
  136.   ssize -= 2;        /* ssize now equal to data portion size */
  137.   segments = 1 + (len - 1) / ssize;    /* # segments  */
  138.  
  139.   while(segments != 0){
  140.     offset += dup_p(&bptmp,bp,offset,ssize);
  141.     if(bptmp == NULLBUF){
  142.       free_q(&result);
  143.       break;
  144.     }
  145.     /* Make room for segmentation header */
  146.     if((bp1 = pushdown(bptmp,2)) == NULLBUF){
  147.       free_p(bptmp);
  148.       free_q(&result);
  149.       break;
  150.     }
  151.     bp1->data[0] = PID_SEGMENT;
  152.     bp1->data[1] = --segments;
  153.     if(offset == ssize)
  154.       bp1->data[1] |= SEG_FIRST;
  155.     enqueue(&result,bp1);
  156.   }
  157.   free_p(bp);
  158.   return result;
  159. }
  160.  
  161. /* Send IP datagrams across an AX.25 link */
  162. int
  163. ax_send(struct mbuf *bp,struct iface *iface,int32 gateway,
  164.     int prec,int del,int tput,int rel) {
  165.   char *hw_addr;
  166.   struct ax25_cb *axp, *axp1;
  167.   struct mbuf *tbp;
  168.   int16 mode;
  169.   struct ip bp1;
  170.   struct arp_tab *arp;
  171.   struct connection conn;
  172.   struct tcp seg;
  173.   struct tcb *tcb;
  174.  
  175.  
  176.   if(gateway == iface->broadcast) /* This is a broadcast IP datagram */
  177.     return (*iface->output)(iface,Ax25multi[0],iface->hwaddr,PID_IP,bp);
  178.  
  179.   if((arp = arp_lookup(ARP_AX25,gateway)) != NULLARP && arp->state == ARP_VALID) {
  180.     hw_addr = arp->hw_addr;
  181.     mode = arp->flags;
  182.   } else
  183.       if((hw_addr = res_arp(iface,ARP_AX25,gateway,bp)) == NULLCHAR)
  184.     return 0;    /* Wait for address resolution */
  185.  
  186.             /* remove 'del' because tos = 16 foreces da_mode (dc0hk.940925)  */
  187.  
  188.   if(/* del || */ (!rel && (mode == DATAGRAM_MODE)) || addreq(hw_addr,Ax25multi[0]))
  189.     mode = DATAGRAM_MODE;
  190.  
  191.   if(mode == DATAGRAM_MODE)
  192.     /* Use UI frame */
  193.     return (*iface->output)(iface,hw_addr,iface->hwaddr,PID_IP,bp);
  194.  
  195.   if((axp = find_ax25(hw_addr,iface->hwaddr)) == NULLAX25) {
  196.     /* Open a new connection */
  197.     if((axp1 = open_ax25(iface,iface->hwaddr,hw_addr,
  198.              AX_ACTIVE,s_arcall,s_atcall,s_ascall,-2)) == NULLAX25)
  199.       goto quit;
  200.     axp1->mode = DGRAM;
  201.   }
  202.  
  203.   if(axp == NULLAX25) {
  204.     axp = axp1;
  205.     if(axp->state == DISCONNECTED)
  206.       setaxstate(axp,CONNECTING);
  207.     axp->user = 0;
  208.   }
  209.  
  210.   dup_p(&tbp,bp,0,len_p(bp));
  211.   if (ntohip(&bp1,&tbp) != -1)
  212.     if (ntohtcp(&seg,&tbp) != -1) {
  213.       conn.local.port = seg.source;
  214.       conn.local.address = bp1.source;
  215.       conn.remote.port = seg.dest;
  216.       conn.remote.address = bp1.dest;
  217.  
  218.       if((tcb = lookup_tcb(&conn)) != NULLTCB && len_p(tbp) > 0
  219.       && tcb->state != TCP_TIME_WAIT) {
  220.     set_timer(&tcb->timer,10 * dur_timer(&tcb->timer));
  221.     if(dur_timer(&tcb->timer) > 300000L)
  222.       set_timer(&tcb->timer,300000L);
  223.     start_timer(&tcb->timer);
  224.       }
  225.   }
  226.   free_p(tbp);
  227.  
  228. /* discard IP frames and send a SOURCE_QUENCH if there already are bytes
  229.  * in the txq - DC0HK.920401 */
  230.   if(len_p(axp->txq) > axp->iface->mtu) {
  231.     ntohip(&bp1,&bp);
  232.     icmp_output(&bp1,bp,ICMP_QUENCH,0,NULL);
  233.     goto quit;
  234.   }
  235.  
  236.   /* Insert the PID */
  237.   if((tbp = pushdown(bp,1)) == NULLBUF)
  238.     goto quit;
  239.  
  240.   tbp->data[0] = (mode == IPCAM_MODE) ? PID_NO_L3 : PID_IP;
  241.  
  242.   if((bp = segmenter(tbp,axp->iface->flags->paclen)) == NULLBUF)
  243.     goto quit;
  244.  
  245.   send_ax25(axp,bp,DGRAM);
  246.   return 0;
  247. quit:
  248.   free_p(bp);
  249.   return -1;
  250. }
  251.  
  252. /* Add header and send connectionless (UI) AX.25 packet.                */
  253. /* Note that the calling order here must match enet_output        */
  254. /* since ARP also uses it.                         */
  255. /*                                      */
  256. /* struct iface *iface;    Interface to use; overrides routing table     */
  257. /* char *dest;             Destination AX.25 address (7 bytes, shifted) */
  258. /* char *source;           Source AX.25 address (7 bytes, shifted)     */
  259. /* int16 pid;              Protocol ID                     */
  260. /* struct mbuf *data;      Data field (follows PID)             */
  261. int
  262. ax_output(struct iface *iface,char *dest,char *source,int16 pid,
  263.       struct mbuf *data) {
  264.   struct mbuf *abp, *cbp;
  265.   struct iface *ifp;
  266.   struct axroute_tab *rp;
  267.   int ndigis = 0, fill = 0;
  268.  
  269. /* discard UI frames if the respective interface is in DAMA slave mode */
  270.     if ( iface->flags->dama_slave ) {
  271.         free_p(data);
  272.         return -1;
  273.         }
  274.  
  275.   /* Allocate mbuf for control and PID fields, and fill in */
  276.   if((cbp = pushdown(data,2)) == NULLBUF) {
  277.     free_p(data);
  278.     return -1;
  279.   }
  280.   cbp->data[0] = UI;
  281.   cbp->data[1] = pid;
  282.  
  283.   if((rp = axroute_tabptr(axptr(dest),0)) != 0)
  284.     for( ; rp; rp = rp->digi) {
  285.       ifp = rp->ifp;
  286.       if(!fill) {
  287.     fill = 1;
  288.     continue;
  289.       }
  290.       if((abp = pushdown(cbp,AXALEN)) == NULLBUF) {
  291.     free_p(cbp);
  292.     return -1;
  293.       }
  294.       memcpy(abp->data,rp->call.call,ALEN);
  295.       abp->data[ALEN] = rp->call.ssid;
  296.       if(ndigis)
  297.     abp->data[ALEN] &= ~E;
  298.       else {
  299.     abp->data[ALEN] |= E;
  300.     ndigis = 1;
  301.       }
  302.       cbp = abp;
  303.     }
  304.   else
  305.     ifp = iface;
  306.  
  307.   if((abp = pushdown(cbp,2 * AXALEN)) == NULLBUF) {
  308.     free_p(cbp);
  309.     return -1;
  310.   }
  311.   memcpy(abp->data,dest,AXALEN);
  312.   abp->data[ALEN] &= ~E;
  313.   abp->data[ALEN] &= ~C;
  314.   memcpy(abp->data + AXALEN,source,AXALEN);
  315.   if(ndigis)
  316.     abp->data[ALEN + AXALEN] &= ~E;
  317.   else
  318.     abp->data[ALEN + AXALEN] |= E;
  319.   abp->data[ALEN + AXALEN] &= ~C;
  320.  
  321.   if (ifp) {
  322.     if (ifp->forw)
  323.       ifp = ifp->forw;
  324.     ifp->rawsndcnt++;
  325.     ifp->lastsent = secclock();
  326.     return (*ifp->raw)(ifp,abp);
  327.   } else {
  328.     free_p(abp);
  329.     return -1;
  330.   }
  331. }
  332.  
  333. static int near
  334. hash_function(char *call,int rel) {
  335.   int32 hashval  = ((int32)(*call++ << 23) & 0x0f000000L);
  336.  
  337.   hashval |= ((int32)(*call++ << 19) & 0x00f00000L);
  338.   hashval |= ((int32)(*call++ << 15) & 0x000f0000L);
  339.   hashval |= ((int32)(*call++ << 11) & 0x0000f000L);
  340.   hashval |= ((int32)(*call++ <<  7) & 0x00000f00L);
  341.   hashval |= ((int32)(*call++ <<  3) & 0x000000f0L);
  342.   hashval |= ((int32)(*call   >>  1) & 0x0000000fL);
  343.   return (int)(hashval % rel);
  344. }
  345.  
  346. struct axroute_tab *
  347. axroute_tabptr(struct ax25_addr *call,int create) {
  348.   struct axroute_tab *rp;
  349.   int hashval = hash_function((char *)call,AXROUTESIZE);
  350.  
  351.   for (rp = axroute_tab[hashval]; rp && !addreq((char *) &rp->call, (char *) call); rp = rp->next) ;
  352.   if (!rp && create) {
  353.     rp = (struct axroute_tab *)mxallocw(sizeof(struct axroute_tab));
  354.     rp->call = *call;
  355.     rp->next = axroute_tab[hashval];
  356.     axroute_tab[hashval] = rp;
  357.   }
  358.   return rp;
  359. }
  360.  
  361. void
  362. axroute_add(struct ax25_cb *cp,int perm)
  363. {
  364.  
  365.   int i, ncalls = 0;
  366.   char  *ap;
  367.   struct axroute_tab *rp, *lastnode = 0;
  368.   struct ax25_addr calls[MAXDIGIS + 1];
  369.  
  370.   for (ap = cp->path + AXALEN; !addreq(ap,cp->iface->hwaddr); ap += AXALEN) ;
  371.  
  372.   do {
  373.     ap += AXALEN;
  374.     if (ap >= cp->path + cp->pathlen) ap = cp->path;
  375.     if (!*ap || addreq(ap,cp->iface->hwaddr)) return;
  376.     for (i = 0; i < ncalls; i++)
  377.       if (addreq((char *) (calls + i), ap)) return;
  378.     calls[ncalls++] = *axptr(ap);
  379.   } while (ap != cp->path);
  380.  
  381.   for (i = 0; i < ncalls; i++) {
  382.     rp = axroute_tabptr(calls + i, 1);
  383.     if (perm || !rp->perm) {
  384.       if (lastnode) {
  385.     rp->digi = lastnode;
  386.     rp->ifp = 0;
  387.       } else {
  388.     rp->digi = 0;
  389.     rp->ifp = cp->iface;
  390.       }
  391.       rp->perm = perm;
  392.     }
  393.     rp->time = currtime;
  394.     lastnode = rp;
  395.   }
  396. }
  397.  
  398. static int near
  399. axroute(struct ax25_cb *cp,struct mbuf *bp) {
  400.   char *dest;
  401.   struct axroute_tab *rp;
  402.   struct iface *ifp;
  403.  
  404.   if (cp && cp->iface)
  405.     ifp = cp->iface;
  406.   else {
  407.     if (bp->data[AXALEN + ALEN] & E)
  408.       dest = bp->data;
  409.     else
  410.       for (dest = bp->data + 2 * AXALEN; ; dest += AXALEN) {
  411.     if (!(dest[ALEN] & REPEATED))
  412.       break;
  413.     if (dest[ALEN] & E) {
  414.       dest = bp->data;
  415.       break;
  416.     }
  417.       }
  418.     rp = axroute_tabptr(axptr(dest),0);
  419.     ifp = (rp && rp->ifp) ? rp->ifp : axroute_default_ifp;
  420.   }
  421.  
  422.   if (ifp && (ifp->flags->dama_slave == 0 || ifp->flags->dama_busy == 0) ) {
  423.     if (ifp->forw)
  424.       ifp = ifp->forw;
  425.     ifp->rawsndcnt++;
  426.     ifp->lastsent = secclock();
  427.     (*ifp->raw)(ifp, bp);
  428.   }
  429.   else
  430.     free_p(bp);
  431.   return;
  432. }
  433.  
  434. static void near
  435. send_packet(struct ax25_cb *cp,int type,int  cmdrsp,struct mbuf * data) {
  436.   int  control = type;
  437.   struct mbuf *bp = ambufw(cp->pathlen + 1);
  438.   char *p = bp->data;
  439.  
  440.   memcpy(p, cp->path, cp->pathlen);
  441.   if (cmdrsp & DST_C) p[ALEN] |= C;
  442.   if (cmdrsp & SRC_C) p[ALEN + AXALEN] |= C;
  443.   p += cp->pathlen;
  444.  
  445.   if (type == I) {
  446.     control |= (cp->vs << 1);
  447.     cp->vs = next_seq(cp->vs);
  448.   }
  449.   if ((type & 3) != U) {
  450.     control |= (cp->vr << 5);
  451.     stop_timer(&cp->t2);
  452.   }
  453.   if (cmdrsp & PF) control |= PF;
  454.   *p++ = control;
  455.  
  456.   if (type == RR || type == REJ || type == UA)
  457.     cp->rnrsent = 0;
  458.   if (type == RNR)
  459.     cp->rnrsent = 1;
  460.   if (type == REJ)
  461.     cp->rejsent = 1;
  462.   if (cmdrsp == POLL)
  463.     cp->polling = 1;
  464.   if (type == I || cmdrsp == POLL)
  465.     start_timer(&cp->t1);
  466.   if (type == I) {
  467.     start_timer(&cp->t3);
  468.     start_timer(&cp->t6);
  469.   }
  470.   bp->cnt = p - bp->data;
  471.   bp->next = data;
  472.   axroute(cp, bp);
  473. }
  474.  
  475. void
  476. ax_recv(struct iface *iface,struct mbuf * bp) {
  477.   char axheader[10*AXALEN+1], (*mpp)[AXALEN], *ap, *cntrlptr;
  478.   int addrsize, pid, multicast = 0;
  479.   struct ax25_cb axp;
  480.  
  481.   if (!(bp && bp->data)) goto discard;
  482.   if (is_bud(bp->data + AXALEN,0)) goto discard;
  483.   for (cntrlptr = bp->data; !(*cntrlptr++ & E); ) ;
  484.   addrsize = (int)(cntrlptr - bp->data);
  485.   if (addrsize <  2 * AXALEN || addrsize >= bp->cnt ||
  486.       addrsize > 10 * AXALEN || addrsize % AXALEN)
  487.     goto discard;
  488.  
  489.   if(addrsize == 2 * AXALEN)
  490.     logaddr(iface,bp->data + AXALEN,cntrlptr[1]); /* fix dc0hk.940920 */
  491.   else {
  492.     for (ap = bp->data + 2 * AXALEN; ap < cntrlptr; ap += AXALEN)
  493.       if (!(ap[ALEN] & REPEATED)) {
  494.     logaddr(iface,ap - AXALEN,cntrlptr[1]); /* fix dc0hk.940920 */
  495.     if (!addreq(ap,iface->hwaddr))
  496.       goto discard;
  497.     ap[ALEN] |= REPEATED;
  498.     switch (iface->flags->digipeat) {
  499.       case 2:
  500.         axproto_recv(iface, bp, 0);
  501.         return;
  502.       case 1:
  503.         axroute(NULLAX25, bp);
  504.         return;
  505.       default:
  506.         goto discard;
  507.     }
  508.       }
  509.     logaddr(iface,ap - AXALEN,cntrlptr[1]); /* fix dc0hk.940920 */
  510.   }
  511.  
  512.   if ((*cntrlptr & ~PF) != UI) {
  513.     if (!addreq(bp->data,iface->hwaddr)
  514.     && (find_ax25(bp->data + AXALEN,bp->data) == NULLAX25))
  515.       goto discard;
  516.     axproto_recv(iface, bp, 1);
  517.     return;
  518.   }
  519.   if (addreq(bp->data,iface->hwaddr))
  520.     goto jump;
  521.  
  522.   /* Examine immediate destination for a multicast address */
  523.   for(mpp = Ax25multi;(*mpp)[0] != '\0';mpp++)
  524.     if(addreq(bp->data,*mpp)){
  525.       multicast = 1;
  526.       goto jump;
  527.     }
  528.   goto discard;
  529.  
  530. jump:
  531.   pullup(&bp, axheader, addrsize + 1);
  532.   pid = PULLCHAR(&bp);
  533.   if (!bp)
  534.     return;
  535.  
  536.   if(!multicast)
  537.     build_path(&axp,iface,axheader,1,0);
  538.  
  539.   switch (pid) {
  540.     case PID_IP:
  541.       if (bp->cnt >= 20) {
  542.     struct arp_tab *arp;
  543.  
  544.     if ((arp = arp_add(         /* src_ipaddr */
  545.          get32(&bp->data[12]),ARP_AX25,axheader + AXALEN,0,0)) != NULLARP) {
  546.       stop_timer(&arp->timer);
  547.       set_timer(&arp->timer,0L);
  548.     }
  549.       }
  550.       ip_route(iface,iface,bp,multicast);
  551.       return;
  552.     case PID_ARP:
  553.       arp_input(iface, bp);
  554.       return;
  555. #ifdef NETROM
  556.     case PID_NETROM:
  557.       nr_nodercv(iface,axheader + AXALEN,bp);
  558.       return;
  559. #endif
  560. #ifdef AX25
  561.     case PID_NO_L3:         /* FLEXNET-Search should now be answered */
  562.       if(!multicast && Attended) {
  563.     iface->flags->dama_busy = 0;
  564.     send_packet(&axp,DM,FINAL,NULLBUF);
  565.     if(iface->flags->dama_slave)
  566.       iface->flags->dama_busy = 1;
  567.       }
  568. #endif
  569.     default:
  570.       goto discard;
  571.   }
  572.  
  573. discard:
  574.   free_p(bp);
  575. }
  576.  
  577. #ifdef AXIP
  578. static int32 axipaddr[NAX25];   /* table of IP addresses of AX.25 interfaces */
  579.  
  580. /*
  581.  * FCS lookup table as generated by fcsgen.c
  582.  */
  583. static int16 near fcstab[256] = {
  584.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  585.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  586.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  587.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  588.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  589.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  590.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  591.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  592.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  593.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  594.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  595.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  596.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  597.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  598.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  599.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  600.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  601.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  602.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  603.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  604.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  605.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  606.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  607.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  608.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  609.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  610.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  611.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  612.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  613.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  614.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  615.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  616. };
  617.  
  618. /* raw routine for sending AX.25 on top of IP */
  619. static int
  620. axip_raw(iface,bp)
  621. struct iface *iface;    /* Pointer to interface control block */
  622. struct mbuf *bp;        /* Data field */
  623. {
  624.      int16 len = len_p(bp), fcs = 0xffff;
  625.      struct mbuf *bp1;
  626.  
  627.      if(dup_p(&bp1,bp,0,len) != len) {
  628.         free_p(bp);
  629.         return -1;
  630.      }
  631.      while (len--)        /* calculate FCS */
  632.        fcs = (fcs >> 8) ^ fcstab[(fcs ^ PULLCHAR(&bp1)) & 0x00ff];
  633.  
  634.      fcs ^= 0xffff;        /* final FCS (is this right?) */
  635.      bp1 = ambufw(sizeof(fcs));
  636.      *bp1->data = fcs & 0xff;
  637.      *(bp1->data+1) = (fcs >> 8) & 0xff;
  638.      bp1->cnt += sizeof(fcs);
  639.      append(&bp,bp1);
  640.      return ip_send(INADDR_ANY,axipaddr[iface->dev],AX25_PTCL,0,0,bp,0,0,0);
  641. }
  642.  
  643. /* Handle AX.25 frames received inside IP according to RFC-1226 */
  644. void
  645. axip_input(iface,ip,bp,rxbroadcast)
  646. struct iface *iface;    /* Input interface */
  647. struct ip *ip;          /* IP header */
  648. struct mbuf *bp;        /* AX.25 frame with FCS */
  649. int rxbroadcast;    /* Accepted for now */
  650. {
  651.     int i;
  652.     struct mbuf *tbp;
  653.     int16 len, f, fcs = 0xffff;
  654.  
  655.     /* Since the AX.25 frame arrived on an interface that does
  656.        not necessarily support AX.25, we have to find a suitable
  657.        AX.25 interface, or drop the packet.
  658.      */
  659.     /* Try to find a matching AX.25 pseudo interface */
  660.     for(i=0; i < NAX25; ++i)
  661.          if(axipaddr[i] == ip->source)
  662.           break;
  663.     if(i == NAX25) {
  664.          /* Here we could still try to pick a real AX.25 interface,
  665.         but that would mean that we are accepting AX.25 frames
  666.         from unknown IP hosts, so we'd rather drop it.
  667.           */
  668.          free_p(bp);
  669.          return;
  670.     }
  671.     for(iface = Ifaces; iface != NULLIF; iface = iface->next) {
  672.          if(iface->raw == axip_raw && iface->dev == i)
  673.           /* found the right AX.25 pseudo interface */
  674.          break;
  675.     }
  676.     if(iface == NULLIF) {
  677.          free_p(bp);
  678.          return;
  679.     }
  680.     iface->rawrecvcnt++;
  681.     iface->lastrecv = secclock();
  682.     len = len_p(bp) - sizeof(fcs);
  683.     if(dup_p(&tbp,bp,0,len) != len) {
  684.          free_p(bp);
  685.          return;
  686.     }
  687.     while(len--)
  688.          fcs = (fcs >> 8) ^ fcstab[(fcs ^ PULLCHAR(&bp)) & 0x00ff];
  689.     fcs ^= 0xffff;
  690.     f = PULLCHAR(&bp);
  691.     f |= (PULLCHAR(&bp) << 8);
  692.     if(fcs == f)
  693.          ax_recv(iface,tbp);
  694.     else
  695.          free_p(tbp);
  696. }
  697. #endif /* AXIP */
  698.  
  699. static void near
  700. reset_t1(cp)
  701. struct ax25_cb *cp;
  702. {
  703.   int32 tmp = 4 * cp->mdev + cp->srt;
  704.  
  705.   if(tmp > 3000 * cp->iface->flags->t1init)
  706.     tmp = 3000 * cp->iface->flags->t1init;
  707.   if(tmp < 2000)
  708.     tmp = 2000;
  709.  
  710.   set_timer(&cp->t1,tmp);
  711. }
  712.  
  713. static void near
  714. inc_t1(cp)
  715. struct ax25_cb *cp;
  716. {
  717.   int32 tmp = (dur_timer(&cp->t1) * 5 + 2) / 4;
  718.  
  719.   if(tmp > 3000 * cp->iface->flags->t1init)
  720.     tmp = 3000 * cp->iface->flags->t1init;
  721.   if(tmp < 2000)
  722.     tmp = 2000;
  723.  
  724.   set_timer(&cp->t1,tmp);
  725. }
  726.  
  727. static int near
  728. space_ax25(struct ax25_cb *cp) {
  729.   int  cnt;
  730.  
  731.   if (cp) {
  732.     switch (cp->state) {
  733.       case CONNECTING:
  734.       case CONNECTED:
  735.     if (!cp->closed) {
  736.       cnt = (cp->cwind - cp->unack) * cp->iface->flags->paclen - (len_p(cp->txq) -1);
  737.       return (cnt > 0) ? cnt : 0;
  738.     }
  739.     }
  740.   }
  741.   return -1;
  742. }
  743.  
  744. static int near
  745. busy(cp)
  746. struct ax25_cb *cp;
  747. {
  748.   return cp->peer ? space_ax25(cp->peer) <= 0
  749.     : len_p(cp->rxq) >= cp->iface->flags->axwindow;
  750. }
  751.  
  752. static void near
  753. send_ack(struct ax25_cb *cp,int cmdrsp) {
  754.   if (busy(cp))
  755.     send_packet(cp, RNR, cmdrsp, NULLBUF);
  756.   else if (!cp->dama && !cp->rejsent
  757.         && (cp->reseq[0].bp || cp->reseq[1].bp || cp->reseq[2].bp ||
  758.         cp->reseq[3].bp || cp->reseq[4].bp || cp->reseq[5].bp ||
  759.         cp->reseq[6].bp || cp->reseq[7].bp))
  760.     send_packet(cp, REJ, cmdrsp, NULLBUF);
  761.   else if (cp->dama
  762.         && (cp->reseq[0].bp || cp->reseq[1].bp || cp->reseq[2].bp ||
  763.         cp->reseq[3].bp || cp->reseq[4].bp || cp->reseq[5].bp ||
  764.         cp->reseq[6].bp || cp->reseq[7].bp))
  765.     send_packet(cp, REJ, cmdrsp, NULLBUF);
  766.   else
  767.     send_packet(cp, RR, cmdrsp, NULLBUF);
  768. }
  769.  
  770. static void near
  771. try_send(struct ax25_cb *cp,int fill_sndq,int mode) {
  772.   int  cnt;
  773.   struct mbuf *bp, *tbp;
  774.  
  775.   stop_timer(&cp->t5);
  776.   while (cp->unack < cp->cwind) {
  777.     if (cp->state != CONNECTED || cp->remotebusy)
  778.       return;
  779.     if (fill_sndq && cp->t_upcall) {
  780.       if((cnt = space_ax25(cp)) > 0) {
  781.     (*cp->t_upcall)(cp, cnt);
  782.     if (cp->unack >= cp->cwind) return;
  783.       }
  784.     }
  785.     if (!cp->txq) return;
  786.     if (mode == STREAM) {
  787.       cnt = len_p(cp->txq);
  788.       if (cnt < cp->iface->flags->paclen) {
  789.     if (cp->unack)
  790.       return;
  791.     if (!cp->peer && cp->sndqtime + cp->iface->flags->t5init * 1000L > msclock()) {
  792.       set_timer(&cp->t5,(cp->sndqtime + cp->iface->flags->t5init * 1000L - msclock()));
  793.       start_timer(&cp->t5);
  794.       return;
  795.     }
  796.       }
  797.       if (cnt > cp->iface->flags->paclen)
  798.     cnt = cp->iface->flags->paclen;
  799.       bp = ambufw(cnt);
  800.       pullup(&cp->txq, bp->data, bp->cnt = cnt);
  801.       tbp = pushdown(bp,1);
  802.       tbp->data[0] = PID_NO_L3;
  803.       bp = tbp;
  804.     } else {
  805.       bp = dequeue(&cp->txq);
  806.     }
  807.     enqueue(&cp->rxasm, bp);
  808.     cp->unack++;
  809.     cp->sndtime[cp->vs] = msclock();
  810.     dup_p(&bp, bp, 0, MAXINT16);
  811.     send_packet(cp, I, CMD, bp);
  812.   }
  813. }
  814.  
  815. static void near
  816. setaxstate(struct ax25_cb *cp,int newstate) {
  817.   int oldstate = cp->state;
  818.  
  819.   cp->state = newstate;
  820.   cp->polling = 0;
  821.   cp->retries = 0;
  822.   cp->flushed = 0;
  823.   stop_timer(&cp->t1);
  824.   stop_timer(&cp->t2);
  825.   stop_timer(&cp->t4);
  826.   stop_timer(&cp->t5);
  827.   if (!cp->dama)
  828.     reset_t1(cp);
  829.  
  830.   switch (newstate) {
  831.     case DISCONNECTED:
  832.       if (cp->peer)
  833.     disc_ax25(cp->peer);
  834.       if (cp->s_upcall)
  835.     (*cp->s_upcall)(cp, oldstate, newstate);
  836.       if (cp->peer && cp->peer->state == DISCONNECTED) {
  837.     if (cp->peer->dama && (--cp->peer->iface->flags->dama_slave == 0))
  838.       dama_off(cp->peer->iface);
  839.     del_ax25(cp->peer);
  840.     cp->peer = cp->peer->peer = NULLAX25;
  841.       }
  842.       break;
  843.     case CONNECTING:
  844.       if (cp->s_upcall)
  845.     (*cp->s_upcall)(cp, oldstate, newstate);
  846.       send_packet(cp, SABM, POLL, NULLBUF);
  847.       break;
  848.     case CONNECTED:
  849.       if (cp->peer && cp->peer->state == DISCONNECTED) {
  850.     send_packet(cp->peer, UA, FINAL, NULLBUF);
  851.     setaxstate(cp->peer, CONNECTED);
  852.       }
  853.       if (cp->s_upcall)
  854.     (*cp->s_upcall)(cp, oldstate, newstate);
  855.       if (!cp->dama)
  856.     try_send(cp, 1, cp->mode);
  857.       break;
  858.     case DISCONNECTING:
  859.       if (cp->peer) {
  860.     disc_ax25(cp->peer);
  861.     cp->peer = cp->peer->peer = NULLAX25;
  862.       }
  863.       if (cp->s_upcall)
  864.     (*cp->s_upcall)(cp, oldstate, newstate);
  865.       send_packet(cp, DISC, POLL, NULLBUF);
  866.       break;
  867.     }
  868. }
  869.  
  870.                                                 /* timers T1 T2 T3 T4 T5 T6 */
  871.  
  872. static void                                     /* t1_timer */
  873. t1_timeout(struct ax25_cb *cp) {
  874.   inc_t1(cp);
  875.   cp->cwind = 1;
  876.   cp->retries++;
  877.  
  878.   switch (cp->state) {
  879.     case DISCONNECTED:
  880.       break;
  881.     case CONNECTING:
  882.       if (cp->peer && cp->peer->state == DISCONNECTED)
  883.     if (cp->retries > 2) {
  884.       cp->reason = LB_TIMEOUT;
  885.       setaxstate(cp, DISCONNECTED);
  886. #ifdef NETROM
  887.       nr_derate(cp);
  888. #endif
  889.     } else
  890.       start_timer(&cp->t1);
  891.       else if (cp->retries > cp->iface->flags->retries) {
  892.     cp->reason = LB_TIMEOUT;
  893.     setaxstate(cp, DISCONNECTED);
  894. #ifdef NETROM
  895.     nr_derate(cp);
  896. #endif
  897.       }
  898.       else
  899.     send_packet(cp, SABM, POLL, NULLBUF);
  900.       break;
  901.     case CONNECTED:
  902.       if (cp->retries > (cp->iface->flags->retries * 2)) {
  903.     cp->reason = LB_TIMEOUT;
  904.     setaxstate(cp, DISCONNECTED);
  905. #ifdef NETROM
  906.     nr_derate(cp);
  907. #endif
  908.       } else
  909.     if (!cp->polling && !cp->remotebusy && cp->unack && !cp->dama
  910.          && len_p(cp->rxasm) - 1 <= cp->iface->flags->pthresh) {
  911.       int  old_vs;
  912.       struct mbuf *bp;
  913.       old_vs = cp->vs;
  914.       cp->vs = (cp->vs - cp->unack) & 7;
  915.       cp->sndtime[cp->vs] = 0;
  916.       dup_p(&bp, cp->rxasm, 0, MAXINT16);
  917.       send_packet(cp, I, POLL, bp);
  918.       cp->vs = old_vs;
  919.     } else if (!cp->dama) {
  920.       send_ack(cp, POLL);
  921.     }
  922.       break;
  923.     case DISCONNECTING:
  924.       if (cp->retries > (cp->iface->flags->retries / 2)) {
  925.     cp->reason = LB_TIMEOUT;
  926.     setaxstate(cp, DISCONNECTED);
  927.     if (cp->dama && (--cp->iface->flags->dama_slave == 0) )
  928.       dama_off(cp->iface);
  929. #ifdef NETROM
  930.     nr_derate(cp);
  931. #endif
  932.       } else
  933.     send_packet(cp, DISC, POLL, NULLBUF);
  934.       break;
  935.     }
  936. }
  937.  
  938. static void                                             /* t2_timer */
  939. t2_timeout(struct ax25_cb *cp) {
  940.   send_ack(cp, RESP);
  941. }
  942.  
  943.  
  944. static void t3_timeout(struct ax25_cb *cp) {            /* t3_timer normal timeout */
  945.   if (!run_timer(&cp->t1) && cp->state == CONNECTED) {
  946.     if(cp->iface->flags->t3disc)
  947.       disc_ax25(cp);
  948.     else {
  949.       if (!cp->dama)
  950.     send_ack(cp, POLL);
  951.     }
  952.   }
  953. }
  954.  
  955. static void                                             /* t4_timer */
  956. t4_timeout(struct ax25_cb *cp) {
  957.  if (!cp->polling && !cp->dama)
  958.     send_ack(cp, POLL);
  959. }
  960.  
  961. static void                                             /* t5_timer */
  962. t5_timeout(struct ax25_cb *cp) {
  963.   try_send(cp, 1, STREAM);
  964. }
  965.  
  966.  
  967. static void t6_timeout(struct ax25_cb *cp) {            /* t6_timer dama timeout */
  968.   if (!run_timer(&cp->t1) && cp->state == CONNECTED && cp->dama) {
  969.     cp->dama = 0;
  970.     if (-- cp->iface->flags->dama_slave == 0)
  971.       dama_off(cp->iface);
  972.  
  973.     if ( cp->iface->flags->dama_slave && (cp->txq || cp->unack)) {
  974.       /* we want to close the connection immediately after T1 is expired */
  975.       cp->retries = cp->iface->flags->retries * 2;
  976.     }
  977.  
  978.     start_timer(&cp->t1);
  979.     disc_ax25(cp);
  980.   }
  981. }
  982.  
  983. /* Look up entry in connection table */
  984. struct ax25_cb *
  985. find_ax25(char *remote,char *local) {
  986.   struct ax25_cb *axp, *axlast = NULLAX25;
  987.  
  988.   /* Search list */
  989.   for(axp = Ax25_cb; axp != NULLAX25; axlast = axp, axp = axp->next){
  990.     if(addreq(axp->path,remote) && addreq(axp->path + AXALEN,local)){
  991.       if(axlast != NULLAX25) {
  992.     /* Move entry to top of the list */
  993.     axlast->next = axp->next;
  994.     axp->next = Ax25_cb;
  995.     Ax25_cb = axp;
  996.       }
  997.       return axp;
  998.     }
  999.   }
  1000.   return NULLAX25;
  1001. }
  1002.  
  1003. static void near
  1004. init_timer(struct ax25_cb *axp)
  1005. {
  1006.   if (axp->iface != NULLIF) {
  1007.     set_timer(&axp->t1,axp->iface->flags->t1init * 1000L);
  1008.     axp->t1.func = (void (*) __ARGS((void *))) t1_timeout;
  1009.     axp->t1.arg = axp;
  1010.  
  1011.     set_timer(&axp->t2,axp->iface->flags->t2init * 1000L);
  1012.     axp->t2.func = (void (*) __ARGS((void *))) t2_timeout;
  1013.     axp->t2.arg = axp;
  1014.  
  1015.     set_timer(&axp->t3,axp->iface->flags->t3init * 1000L);
  1016.     axp->t3.func = (void (*) __ARGS((void *))) t3_timeout;
  1017.     axp->t3.arg = axp;
  1018.  
  1019.     set_timer(&axp->t4,axp->iface->flags->t4init * 1000L);
  1020.     axp->t4.func = (void (*) __ARGS((void *))) t4_timeout;
  1021.     axp->t4.arg = axp;
  1022.  
  1023.     set_timer(&axp->t5,axp->iface->flags->t5init * 1000L);
  1024.     axp->t5.func = (void (*) __ARGS((void *))) t5_timeout;
  1025.     axp->t5.arg = axp;
  1026.  
  1027.     set_timer(&axp->t6,axp->iface->flags->t6init * 1000L);
  1028.     axp->t6.func = (void (*) __ARGS((void *))) t6_timeout;
  1029.     axp->t6.arg = axp;
  1030.   }
  1031. }
  1032.  
  1033. /* Create an ax25 control block. Allocate a new structure, if necessary,
  1034.  * and fill it with all the defaults. The caller
  1035.  * is still responsible for filling in the reply address
  1036.  */
  1037. static struct ax25_cb * near
  1038. cr_ax25(char *remote,char *local,struct iface *iface) {
  1039.   struct ax25_cb *axp;
  1040.  
  1041.   if(remote == NULLCHAR)
  1042.     return NULLAX25;
  1043.  
  1044.   if((axp = find_ax25(remote,local)) == NULLAX25){
  1045.     /* Not already in table; create an entry */
  1046.     axp = (struct ax25_cb *)mxallocw(sizeof(struct ax25_cb));
  1047.     axp->next = Ax25_cb;
  1048.     Ax25_cb = axp;
  1049.   }
  1050.   axp->state = DISCONNECTED;
  1051.   axp->cwind = 1;
  1052.   axp->user = -1;
  1053.   axp->iface = iface;
  1054.   init_timer(axp);
  1055.  
  1056.   /* Always to a receive and state upcall as default */
  1057.   /* Also bung in a default transmit upcall - in case */
  1058.   axp->r_upcall = s_arcall;
  1059.   axp->t_upcall = s_atcall;
  1060.   axp->s_upcall = s_ascall;
  1061.   axp->peer = NULLAX25;
  1062.   return axp;
  1063. }
  1064.  
  1065.  
  1066.  
  1067. /* Open an AX.25 connection */
  1068. struct ax25_cb *
  1069. open_ax25(iface,local,remote,mode,r_upcall,t_upcall,s_upcall,user)
  1070. struct iface *iface;        /* Interface */
  1071. char *local;            /* Local address */
  1072. char *remote;            /* Remote address */
  1073. int mode;            /* active/passive/server */
  1074. void (*r_upcall)();        /* Receiver upcall handler */
  1075. void (*t_upcall)();        /* Transmitter upcall handler */
  1076. void (*s_upcall)();        /* State-change upcall handler */
  1077. int user;            /* User linkage area */
  1078. {
  1079.     struct ax25_cb *axp = NULLAX25;
  1080.     char remtmp[AXALEN], path[3*AXALEN];
  1081.  
  1082.     if(remote == NULLCHAR){
  1083.         remote = remtmp;
  1084.         setcall(remote," ");
  1085.     }
  1086.     if((axp = find_ax25(remote,local)) != NULLAX25 && axp->state != DISCONNECTED)
  1087.         return NULLAX25;    /* Only one to a customer */
  1088.     if(axp == NULLAX25 && (axp = cr_ax25(remote,local,iface)) == NULLAX25)
  1089.         return NULLAX25;
  1090.  
  1091.     axp->r_upcall = r_upcall;
  1092.     axp->t_upcall = t_upcall;
  1093.     axp->s_upcall = s_upcall;
  1094.     axp->user = (user == -2) ? -1 : user;
  1095.  
  1096.     memcpy(path,remote,AXALEN);
  1097.     path[ALEN] &= ~E;
  1098.     memcpy(path + AXALEN,local,AXALEN);
  1099.     path[ALEN + AXALEN] |= E;
  1100.  
  1101.     switch(mode) {
  1102.     case AX_SERVER:
  1103.         axp->clone = 1;
  1104.     case AX_PASSIVE:    /* Note fall-thru */
  1105.         axp->state = LISTEN;
  1106.         return axp;
  1107.     case AX_ACTIVE:
  1108.         break;
  1109.     }
  1110.  
  1111.     build_path(axp,NULLIF,path,0,1);
  1112.  
  1113.     if(!axp->iface) {
  1114.         del_ax25(axp);
  1115.         return NULLAX25;
  1116.     }
  1117.  
  1118.     if(user == -2) {
  1119.         addrcp(axp->path + AXALEN,axp->iface->hwaddr);
  1120.         axp->path[axp->pathlen - 1] |= E;
  1121.         axroute_add(axp,0);
  1122.     }
  1123.  
  1124.     switch(axp->state){
  1125.     case DISCONNECTED:
  1126.         if(user != -2)
  1127.             setaxstate(axp,CONNECTING);
  1128.         break;
  1129.     case CONNECTING:
  1130.         free_q(&axp->txq);
  1131.         free_q(&axp->rxasm);
  1132.         break;
  1133.     case DISCONNECTING:    /* Ignore */
  1134.         break;
  1135.     case CONNECTED:
  1136.         free_q(&axp->txq);
  1137.         setaxstate(axp,CONNECTING);
  1138.         break;
  1139.     }
  1140.     return axp;
  1141. }
  1142.  
  1143.  
  1144.  
  1145.  
  1146. int
  1147. send_ax25(struct ax25_cb *cp,struct mbuf *bp,int pid) {
  1148.   /* pid: used as the connection mode indicator */
  1149.   int16 cnt;
  1150.  
  1151.   if (!(cp && bp)) {
  1152.     free_p(bp);
  1153.     return (-1);
  1154.   }
  1155.   cp->mode = pid;
  1156.   switch (cp->state) {
  1157.     case DISCONNECTED:
  1158.       free_p(bp);
  1159.       return (-1);
  1160.     case CONNECTING:
  1161.     case CONNECTED:
  1162.       if (!cp->closed) {
  1163.     if ((cnt = len_p(bp)) != 0) {
  1164.       if (pid == 0)
  1165.         append(&cp->txq, bp);
  1166.       else
  1167.         enqueue(&cp->txq, bp);
  1168.       cp->sndqtime = msclock();
  1169.       try_send(cp, 0, pid);
  1170.       }
  1171.       return (int)cnt;
  1172.     }
  1173.     case DISCONNECTING:
  1174.       free_p(bp);
  1175.       return (-1);
  1176.   }
  1177.   return (-1);
  1178. }
  1179.  
  1180. /* Receive incoming data on an AX.25 connection */
  1181. struct mbuf *
  1182. recv_ax25(struct ax25_cb *axp,int16 cnt) {
  1183.   struct mbuf *bp;
  1184.  
  1185.   if(!axp->rxq)
  1186.     return NULLBUF;
  1187.  
  1188.   if(cnt == 0 || axp->mode == DGRAM || axp->rcvcnt <= cnt){
  1189.     /* This means we want it all */
  1190.     bp = dequeue(&axp->rxq);
  1191.     cnt = len_p(bp);
  1192.     axp->rxq = NULLBUF;
  1193.   }
  1194.   else {
  1195.     bp = ambufw(cnt);
  1196.     bp->cnt = pullup(&axp->rxq,bp->data,cnt);
  1197.   }
  1198.   axp->rcvcnt -= cnt;
  1199.  
  1200.   /* If this has un-busied us, send a RR to reopen the window */
  1201.   if(axp->rnrsent && !busy(axp)) send_ack(axp,RESP);
  1202.  
  1203.   return bp;
  1204. }
  1205.  
  1206. /* Close an AX.25 connection */
  1207. int
  1208. disc_ax25(struct ax25_cb *axp) {
  1209.   if(axp == NULLAX25)
  1210.     return -1;
  1211.   if(axp->closed)
  1212.     return -1;
  1213.   axp->closed = 1;
  1214.   switch(axp->state){
  1215.     case LISTEN:
  1216.       del_ax25(axp);
  1217.     case DISCONNECTED:
  1218.     case DISCONNECTING:
  1219.       return -1;;
  1220.     case CONNECTED:
  1221.       if(!axp->txq && !axp->unack)
  1222.     setaxstate(axp,DISCONNECTING);
  1223.       return 0;
  1224.     case CONNECTING:
  1225.       setaxstate(axp, DISCONNECTED);
  1226.       return 0;
  1227.     }
  1228.   return -1;
  1229. }
  1230.  
  1231.  
  1232. /* Abruptly terminate an AX.25 connection */
  1233. int
  1234. reset_ax25(struct ax25_cb *axp) {
  1235.   void (*upcall)();
  1236.  
  1237.   if (axp == NULLAX25)
  1238.     return -1;
  1239.  
  1240.   if (axp->dama && (--axp->iface->flags->dama_slave == 0) ) {
  1241.     dama_off(axp->iface);
  1242.     axp->dama = 0;
  1243.   }
  1244.  
  1245.   upcall = axp->s_upcall;
  1246.   axp->reason = LB_DM;
  1247.   setaxstate(axp,DISCONNECTED);
  1248.   /* Clean up if the standard upcall isn't in use */
  1249.   if(upcall != s_ascall)
  1250.     del_ax25(axp);
  1251.   return 0;
  1252. }
  1253.  
  1254. /* Remove entry from connection table */
  1255. void
  1256. del_ax25(struct ax25_cb *conn) {
  1257.   struct ax25_cb *axp, *axlast = NULLAX25;
  1258.   int i;
  1259.  
  1260.   for(axp = Ax25_cb; axp != NULLAX25; axlast=axp,axp = axp->next){
  1261.     if(axp == conn)
  1262.       break;
  1263.   }
  1264.   if(axp == NULLAX25)
  1265.     return;    /* Not found */
  1266.  
  1267.   /* Remove from list */
  1268.   if(axlast != NULLAX25)
  1269.     axlast->next = axp->next;
  1270.   else
  1271.     Ax25_cb = axp->next;
  1272.  
  1273.   /* Timers should already be stopped, but just in case... */
  1274.   stop_timer(&axp->t1);
  1275.   stop_timer(&axp->t2);
  1276.   stop_timer(&axp->t3);
  1277.   stop_timer(&axp->t4);
  1278.   stop_timer(&axp->t5);
  1279.   stop_timer(&axp->t6);
  1280.  
  1281.   /* Free allocated resources */
  1282.   for(i = 0; i < 8; i++) free_p(axp->reseq[i].bp);
  1283.   free_q(&axp->txq);
  1284.   free_q(&axp->rxasm);
  1285.   free_q(&axp->rxq);
  1286.   free_q(&axp->segasm);
  1287.   xfree((char *)axp);
  1288. }
  1289.  
  1290. /* Verify that axp points to a valid ax25 control block */
  1291. int
  1292. ax25val(struct ax25_cb *axp) {
  1293.   struct ax25_cb *axp1;
  1294.  
  1295.   if(axp == NULLAX25)
  1296.     return 0;            /* Null pointer can't be valid */
  1297.   for(axp1 = Ax25_cb; axp1 != NULLAX25; axp1 = axp1->next)
  1298.     if(axp1 == axp)
  1299.       return 1;
  1300.   return 0;
  1301. }
  1302.  
  1303. /* Force a retransmission */
  1304. int
  1305. kick_ax25(struct ax25_cb *axp) {
  1306.   if(!ax25val(axp))
  1307.     return -1;
  1308.   t1_timeout(axp);
  1309.   return 0;
  1310. }
  1311.  
  1312. static void near
  1313. dama_flush(struct ax25_cb *cp) {
  1314.   struct mbuf *bp1, *qp;
  1315.   struct ax25_cb *axp;
  1316.  
  1317.   cp->iface->flags->dama_busy = 0;
  1318.  
  1319.   if (cp->unack) {
  1320.     cp->flushed = 1;
  1321.     cp->vs = (cp->vs - cp->unack) & 7;
  1322.     for (qp = cp->rxasm; qp; qp = qp->anext ) {
  1323.       cp->sndtime[cp->vs] = 0;
  1324.       dup_p(&bp1, qp, 0, MAXINT16);
  1325.       send_packet(cp, I, CMD, bp1);
  1326.     }
  1327.   } else {
  1328.     try_send(cp,0,cp->mode);
  1329.     if ( cp->unack )        /* set 'flushed' state only if I-frame(s) were */
  1330.     cp->flushed = 1;    /* generated by calling try_send()             */
  1331.   }
  1332.  
  1333.   for (axp = Ax25_cb; axp != NULLAX25; axp = axp->next) {
  1334.     if (cp->iface == axp->iface) {
  1335.       if ( (axp != cp) && (!axp->flushed) ) {
  1336.     if (axp->unack) {
  1337.       axp->flushed = 1;
  1338.       axp->vs = (axp->vs - axp->unack) & 7;
  1339.       for (qp = axp->rxasm; qp; qp = qp->anext ) {
  1340.         axp->sndtime[axp->vs] = 0;
  1341.         dup_p(&bp1, qp, 0, MAXINT16);
  1342.         send_packet(axp, I, CMD, bp1);
  1343.       }
  1344.     }
  1345.     else {
  1346.       try_send(axp,0,cp->mode);
  1347.       if ( axp->unack )
  1348.         axp->flushed = 1;   /* see above, same procedure */
  1349.     }
  1350.       }
  1351.       if (axp->state == CONNECTING) {
  1352.     axp->retries++;
  1353.     if (axp->retries > axp->iface->flags->retries) {
  1354.       axp->reason = LB_TIMEOUT;
  1355.       setaxstate(axp, DISCONNECTED);
  1356. #ifdef NETROM
  1357.       nr_derate(axp);
  1358. #endif
  1359.     } else {
  1360.     send_packet(axp,SABM,POLL,NULLBUF);
  1361.     }
  1362.       }
  1363.     }
  1364.   }
  1365. }
  1366.  
  1367.  
  1368. /*
  1369.  * setcall - convert callsign plus substation ID of the form
  1370.  * "KA9Q-0" to AX.25 (shifted) address format
  1371.  *   Address extension bit is left clear
  1372.  *   Return -1 on error, 0 if OK
  1373.  */
  1374. int
  1375. setcall(char *out,char *call)
  1376. {
  1377.   int csize, i;
  1378.   unsigned ssid;
  1379.   char c, *dp;
  1380.  
  1381.   if(out == NULLCHAR || call == NULLCHAR || *call == '\0')
  1382.     return -1;
  1383.  
  1384.   /* Find dash, if any, separating callsign from ssid
  1385.    * Then compute length of callsign field and make sure
  1386.    * it isn't excessive
  1387.    */
  1388.   if((dp = strchr(call,'-')) == NULLCHAR)
  1389.     csize = strlen(call);
  1390.   else
  1391.     csize = (int)(dp - call);
  1392.  
  1393.   if(csize > ALEN)
  1394.     return -1;
  1395.  
  1396.   /* Now find and convert ssid, if any */
  1397.   if(dp != NULLCHAR) {
  1398.     dp++;   /* skip dash */
  1399.     ssid = atoi(dp);
  1400.     if(ssid > 15)
  1401.       return -1;
  1402.   }
  1403.   else
  1404.     ssid = 0;
  1405.  
  1406.   /* Copy upper-case callsign, left shifted one bit */
  1407.   for(i = 0; i < csize; i++) {
  1408.     c = *call++;
  1409.     if(islower(c))
  1410.     c = toupper(c);
  1411.     *out++ = c << 1;
  1412.   }
  1413.  
  1414.   /* Pad with shifted spaces if necessary */
  1415.   for(; i < ALEN; i++)
  1416.     *out++ = ' ' << 1;
  1417.  
  1418.   /* Insert substation ID field and set reserved bits */
  1419.   *out = 0x60 | (ssid << 1);
  1420.   *out |= E;
  1421.   return 0;
  1422. }
  1423.  
  1424. int
  1425. addreq(char *a,char *b)
  1426. {
  1427.   if (*a++ != *b++) return 0;
  1428.   if (*a++ != *b++) return 0;
  1429.   if (*a++ != *b++) return 0;
  1430.   if (*a++ != *b++) return 0;
  1431.   if (*a++ != *b++) return 0;
  1432.   if (*a++ != *b++) return 0;
  1433.   return (*a & SSID) == (*b & SSID);
  1434. }
  1435.  
  1436. void
  1437. addrcp(char *to,char *from)
  1438. {
  1439.   *to++ = *from++;
  1440.   *to++ = *from++;
  1441.   *to++ = *from++;
  1442.   *to++ = *from++;
  1443.   *to++ = *from++;
  1444.   *to++ = *from++;
  1445.   *to = (*from & SSID) | 0x60;
  1446. }
  1447.  
  1448. /* Convert encoded AX.25 address to printable string */
  1449. char *
  1450. pax25(char *e,char *addr) {
  1451.   int i;
  1452.   char c, *cp = e;
  1453.  
  1454.   for(i=ALEN;i != 0;i--){
  1455.     c = (*addr++ >> 1) & 0x7f;
  1456.     if(c != ' ')
  1457.       *cp++ = c;
  1458.   }
  1459.   if ((*addr & SSID) != 0)
  1460.     sprintf(cp,"-%d",(*addr >> 1) & 0xf);   /* ssid */
  1461.   else
  1462.     *cp = '\0';
  1463.   return e;
  1464. }
  1465.  
  1466. static int near
  1467. put_reseq(struct ax25_cb *cp,struct mbuf *bp,int ns) {
  1468.   char  *p;
  1469.   int  cnt, sum;
  1470.   struct axreseq *rp;
  1471.   struct mbuf *tp;
  1472.  
  1473.   if (next_seq(ns) == cp->vr) return 0;
  1474.  
  1475.   rp = &cp->reseq[ns];
  1476.   if (rp->bp) return 0;
  1477.   for (sum = 0, tp = bp; tp; tp = tp->next) {
  1478.     cnt = tp->cnt;
  1479.     p = tp->data;
  1480.     while (cnt--)
  1481.       sum += uchar(*p++);
  1482.   }
  1483.   if (ns != cp->vr && sum == rp->sum) return 0;
  1484.   rp->bp = bp;
  1485.   rp->sum = sum;
  1486.   return 1;
  1487. }
  1488.  
  1489. void
  1490. build_path(struct ax25_cb *cp,struct iface * ifp,
  1491.       char *newpath,int reverse,int newsrt) {
  1492.  
  1493.   char  buf[10*AXALEN];
  1494.   int  len, ndigi;
  1495.   char  *ap, *tp, *myaddr = 0;
  1496.   struct axroute_tab *rp = 0;
  1497.  
  1498.   /*** find address length and copy address into control block ***/
  1499.  
  1500.   for (ap = newpath; !(ap[ALEN] & E); ap += AXALEN) ;
  1501.   cp->pathlen = (int)(ap - newpath + AXALEN);
  1502.  
  1503.   if (reverse) {
  1504.     addrcp(cp->path, newpath + AXALEN);
  1505.     addrcp(cp->path + AXALEN, newpath);
  1506.     for (tp = cp->path + 2 * AXALEN;
  1507.          tp < cp->path + cp->pathlen;
  1508.          tp += AXALEN, ap -= AXALEN)
  1509.       addrcp(tp, ap);
  1510.   } else {
  1511.     memcpy(cp->path, newpath, cp->pathlen);
  1512.   }
  1513.   /*** store iface pointer into control block ***/
  1514.   cp->iface = ifp;
  1515.  
  1516.   /*** save address for autorouting ***/
  1517.   if(reverse && ifp) axroute_add(cp,0);
  1518.  
  1519.   /*** find my digipeater address (use the last one) ***/
  1520.   for (ap = cp->path + 2 * AXALEN; ap < cp->path + cp->pathlen; ap += AXALEN) {
  1521.     if(!ifp) {
  1522.       for (ifp = Ifaces; ifp; ifp = ifp->next)
  1523.     if (ifp->output == ax_output && addreq(ifp->hwaddr,ap))
  1524.       break;
  1525.     }
  1526.     if (addreq(ap,ifp->hwaddr)) myaddr = ap;
  1527.   }
  1528.  
  1529.   if(!reverse) {
  1530.     /*** autorouting, remove all digipeaters before me ***/
  1531.     if (myaddr && myaddr > cp->path + 2 * AXALEN) {
  1532.       len = (int)((cp->path + cp->pathlen) - myaddr);
  1533.       memcpy(buf, myaddr, len);
  1534.       myaddr = cp->path + 2 * AXALEN;
  1535.       memcpy(myaddr, buf, len);
  1536.       cp->pathlen = 2 * AXALEN + len;
  1537.     }
  1538.     /*** add necessary digipeaters and find interface ***/
  1539.     ap = myaddr ? myaddr + AXALEN : cp->path + 2 * AXALEN;
  1540.     rp = axroute_tabptr(axptr((ap >= cp->path + cp->pathlen) ? cp->path : ap), 0);
  1541.     for (; rp; rp = rp->digi) {
  1542.       if (rp->digi && cp->pathlen < sizeof(cp->path)) {
  1543.     len = (int)((cp->path + cp->pathlen) - ap);
  1544.     if (len)
  1545.       memcpy(buf, ap, len);
  1546.     addrcp(ap, (char *) &rp->digi->call);
  1547.     if (len)
  1548.       memcpy(ap + AXALEN, buf, len);
  1549.     cp->pathlen += AXALEN;
  1550.       }
  1551.       cp->iface = rp->ifp;
  1552.     }
  1553.  
  1554.     if(!cp->iface)
  1555.       cp->iface = axroute_default_ifp;
  1556.  
  1557.     /*** replace my address with hwaddr of interface ***/
  1558.     if(myaddr != 0) {
  1559.       addrcp(cp->pathlen == 2 * AXALEN ? cp->path + AXALEN : cp->path + 2 * AXALEN,
  1560.       cp->iface ? cp->iface->hwaddr : Mycall);
  1561.     }
  1562.   }
  1563.  
  1564.   /*** clear all address bits ***/
  1565.  
  1566.   for (ap = cp->path; ap < cp->path + cp->pathlen; ap += AXALEN)
  1567.     ap[ALEN] = (ap[ALEN] & SSID) | 0x60;
  1568.  
  1569.   /*** set REPEATED bits for all digipeaters before and including me ***/
  1570.  
  1571.   if (myaddr)
  1572.     for (ap = cp->path + 2 * AXALEN; ap <= myaddr; ap += AXALEN)
  1573.       ap[ALEN] |= REPEATED;
  1574.  
  1575.   /*** mark end of address field ***/
  1576.   cp->path[cp->pathlen-1] |= E;
  1577.  
  1578.   if(!newsrt) return;
  1579.  
  1580.   init_timer(cp);
  1581.  
  1582.   /*** estimate round trip time ***/
  1583.   if (myaddr)
  1584.     ndigi = (int)((cp->path + cp->pathlen - myaddr) / AXALEN) + 1;
  1585.   else
  1586.     ndigi = cp->pathlen / AXALEN;
  1587.  
  1588.   cp->srt = (500 * cp->iface->flags->t1init * ndigi);
  1589.   cp->mdev = 0;
  1590.  
  1591.   reset_t1(cp);
  1592. }
  1593.  
  1594. int
  1595. is_bud(char *icall,int store) {
  1596. #define BTABLESIZE 23
  1597.  
  1598.   char bcall[AXALEN];
  1599.   struct btable_t { struct btable_t *next;
  1600.             char call[AXALEN];
  1601.           };
  1602.   static struct btable_t *btable[BTABLESIZE], *p, **tp;
  1603.  
  1604.   memcpy(bcall,icall,AXALEN);
  1605.   bcall[ALEN] = '0' << 1;
  1606.  
  1607.   tp = btable + hash_function(bcall,BTABLESIZE);
  1608.  
  1609.   for (p = *tp; p && !addreq(p->call, bcall); p = p->next) ;
  1610.  
  1611.   if (!p && store) {
  1612.     p = mxallocw(sizeof(*p));
  1613.     addrcp(p->call,bcall);
  1614.     p->next = *tp;
  1615.     *tp = p;
  1616.   }
  1617.   return (int) (p != 0);
  1618. }
  1619.  
  1620. static int near
  1621. is_flexnet(char *call,int store) {
  1622. #define FTABLESIZE 23
  1623.  
  1624.   struct ftable_t { struct ftable_t *next;
  1625.             char call[AXALEN];
  1626.           };
  1627.   static struct ftable_t *ftable[FTABLESIZE], *p, **tp;
  1628.  
  1629.   tp = ftable + hash_function(call,FTABLESIZE);
  1630.  
  1631.   for (p = *tp; p && !addreq(p->call, call); p = p->next) ;
  1632.  
  1633.   if (!p && store) {
  1634.     p = mxallocw(sizeof(*p));
  1635.     addrcp(p->call, call);
  1636.     p->next = *tp;
  1637.     *tp = p;
  1638.   }
  1639.   return (int) (p != 0);
  1640. }
  1641.  
  1642. static int near
  1643. axproto_recv(struct iface *ifp,struct mbuf *bp,char repeated) {
  1644.   int cmdrsp, control, for_me, nr, ns, type, processed, seg, pid, ipcam;
  1645.   int dama_flag = DAMA - (bp->data[AXALEN+ALEN] & DAMA);
  1646.   struct ax25_cb *cp, *cpp;
  1647.   char *cntrlptr, temp[AXALEN], newcon;
  1648.  
  1649.   for(cntrlptr = bp->data + AXALEN;!(cntrlptr[ALEN] & E);cntrlptr += AXALEN) ;
  1650.  
  1651.   cntrlptr += AXALEN;
  1652.   control = uchar(*cntrlptr);
  1653.  
  1654.   if (control & 1) {
  1655.     if (control & 2)
  1656.       type = control & ~PF;
  1657.     else
  1658.       type = control & 0xf;
  1659.   } else
  1660.     type = I;
  1661.  
  1662.   for_me = addreq(bp->data,ifp->hwaddr);
  1663.   if((cp = find_ax25(bp->data + AXALEN,bp->data)) != NULLAX25 && !for_me) {
  1664.     for_me = 2;
  1665.     if(type == SABM && repeated) {
  1666.       free_p(bp);
  1667.       return;
  1668.     }
  1669.   }
  1670.   if (!for_me && (type == UI || addreq(bp->data, bp->data + AXALEN)
  1671.     || is_flexnet(bp->data, 0) || is_flexnet(bp->data + AXALEN, 0))) {
  1672.     axroute(NULLAX25, bp);
  1673.     return;
  1674.   }
  1675.   if((pid = uchar(cntrlptr[1])) == PID_FLEXNET)
  1676.     is_flexnet(bp->data + AXALEN, 1);       /* */
  1677.  
  1678.   if ((bp->data[ALEN] & C) == (bp->data[ALEN + AXALEN] & C))
  1679.     cmdrsp = VERS1;
  1680.   else
  1681.     cmdrsp = ((bp->data[ALEN] & C) ? DST_C : SRC_C) | (control & PF);
  1682.  
  1683.   if(cp) {
  1684.     if (for_me == 1 || (for_me == 2 && cp->peer != NULLAX25)) {
  1685.       build_path(cp,ifp,bp->data,1,0);
  1686.     } else {
  1687.       addrcp(temp,bp->data);            /* if the frame is not directly for */
  1688.       addrcp(bp->data,ifp->hwaddr);        /* us, but for a user of our gateway */
  1689.       build_path(cp,ifp,bp->data,1,0);        /* we MUST change the address field to */
  1690.       addrcp(cp->path + AXALEN,temp);        /* make axrouting work! */
  1691.       if(cp->pathlen == 2 * AXALEN)        /* DB3FL.910110 */
  1692.     cp->path[AXALEN + ALEN] |= E;        /* set address field end bit */
  1693.     }
  1694.     cpp = cp->peer;
  1695.     newcon = 0;
  1696.   } else {
  1697.     cp = cr_ax25(bp->data + AXALEN,bp->data,ifp);
  1698.     build_path(cp, ifp, bp->data, 1, 1);
  1699.     newcon = 1;
  1700.  
  1701.     if (!for_me) {
  1702.       cp->peer = cpp = cr_ax25(bp->data,bp->data + AXALEN,NULLIF);
  1703.       cpp->peer = cp;
  1704.       build_path(cpp, NULLIF, bp->data, 0, 1);
  1705.     } else
  1706.       cpp = NULLAX25;
  1707.   }
  1708.   if (type == SABM) {
  1709.     int  i;
  1710.     build_path(cp, ifp, bp->data, 1, 0);
  1711.     if (cp->unack)
  1712.       start_timer(&cp->t1);
  1713.     else
  1714.       stop_timer(&cp->t1);
  1715.     stop_timer(&cp->t2);
  1716.     stop_timer(&cp->t4);
  1717.     cp->polling = 0;
  1718.     cp->rnrsent = 0;
  1719.     cp->rejsent = 0;
  1720.     cp->remotebusy = 0;
  1721.     cp->vr = 0;
  1722.     cp->vs = cp->unack;
  1723.     cp->retries = 0;
  1724.     cp->flushed = 0;
  1725.     for (i = 0; i < 8; i++)
  1726.       if (cp->reseq[i].bp) {
  1727.     free_q(&cp->reseq[i].bp);
  1728.     cp->reseq[i].bp = NULLBUF;
  1729.       }
  1730.     if (dama_flag) {
  1731.       cp->dama = 1;
  1732.       if (newcon)
  1733.     dama_on(cp->iface);
  1734.     } else
  1735.       cp->dama = 0;
  1736.   }
  1737.  
  1738.   ipcam = 0;
  1739.   if (type == I) {
  1740.     /* IPCAM-feature - DB3FL.910104 */
  1741.     if((for_me == 1 || (for_me == 2 && cp->peer == NULLAX25))
  1742.     && pid == PID_NO_L3
  1743.     && uchar(cntrlptr[2]) == 0x45
  1744.     && uchar(cntrlptr[3]) == 0x00
  1745.     && uchar(cntrlptr[4]) < 0x02) {
  1746.       cntrlptr[1] = PID_IP;
  1747.       pid = PID_IP;
  1748.       ipcam = 1;
  1749.     }
  1750.     cp->mode = (pid == PID_NO_L3) ? STREAM : DGRAM;
  1751.     if(cpp)
  1752.       cpp->mode = cp->mode;
  1753.  
  1754.     if(pid == PID_IP) {
  1755.       struct arp_tab *arp;
  1756.  
  1757.       if ((arp = arp_add(        /* src_ipaddr */
  1758.         get32(&cntrlptr[14]),ARP_AX25,cp->path,0,1+ipcam)) != NULLARP) {
  1759.         stop_timer(&arp->timer);
  1760.         set_timer(&arp->timer,0L);
  1761.       }
  1762.     }
  1763.     start_timer(&cp->t3);
  1764.     start_timer(&cp->t6);
  1765.   }
  1766.  
  1767.   /* every poll from master retriggers T6 timeout */
  1768.   if (cp->dama)
  1769.     start_timer(&cp->t6);
  1770.  
  1771.   if ((for_me == 0) && (cp->iface->flags->dama_slave))
  1772.     cp->iface->flags->dama_busy = 1;
  1773.  
  1774.   switch (cp->state) {
  1775.     case DISCONNECTED:
  1776.       if (for_me == 1 || (for_me == 2 && cp->peer == NULLAX25)) {
  1777.     if (type == SABM && cmdrsp != VERS1 && cp->r_upcall) {
  1778.       if (cp->dama)
  1779.         cp->iface->flags->dama_busy = 0;
  1780.       send_packet(cp, UA, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1781.       if (cp->dama)
  1782.         cp->iface->flags->dama_busy = 1;
  1783.       setaxstate(cp, CONNECTED);
  1784.       start_timer(&cp->t3);
  1785.       start_timer(&cp->t6);
  1786.     } else {
  1787.       if (cmdrsp != RESP && cmdrsp != FINAL) {
  1788.         if (cp->dama)
  1789.           cp->iface->flags->dama_busy = 0;
  1790.         send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1791.         if (cp->dama)
  1792.           cp->iface->flags->dama_busy = 1;
  1793.       }
  1794.       del_ax25(cp);
  1795.     }
  1796.       } else {
  1797.       if (type == SABM && cmdrsp != VERS1 && cpp->state == DISCONNECTED) {
  1798.         setaxstate(cpp, CONNECTING);
  1799.     } else if (type == SABM && cmdrsp != VERS1 && cpp->state == CONNECTING) {
  1800.         build_path(cpp, NULLIF, bp->data, 0, 1);
  1801.         send_packet(cpp, SABM, POLL, NULLBUF);
  1802.     } else {
  1803.       if (cmdrsp != RESP && cmdrsp != FINAL)
  1804.         send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1805.       if (cpp->state == DISCONNECTED) {
  1806.         del_ax25(cpp);
  1807.         del_ax25(cp);
  1808.       }
  1809.     }
  1810.       }
  1811.       break;
  1812.  
  1813.     case CONNECTING:
  1814.       switch (type) {
  1815.     case I:
  1816.     case RR:
  1817.     case RNR:
  1818.     case REJ:
  1819.       break;
  1820.     case SABM:
  1821.       if (cmdrsp != VERS1) {
  1822.         if (cp->dama)
  1823.           cp->iface->flags->dama_busy = 0;
  1824.         send_packet(cp, UA, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1825.         if (cp->dama)
  1826.           cp->iface->flags->dama_busy = 1;
  1827.         setaxstate(cp, CONNECTED);
  1828.       }
  1829.       break;
  1830.     case UA:
  1831.       if (cmdrsp != VERS1) {
  1832.         start_timer(&cp->t3);
  1833.         start_timer(&cp->t6);
  1834.         if (dama_flag) {
  1835.           cp->dama = 1;
  1836.           dama_on(cp->iface);
  1837.         } else {
  1838.           cp->dama = 0;
  1839.         }
  1840.         setaxstate(cp, CONNECTED);
  1841.       } else {
  1842.         if (cpp && cpp->state == DISCONNECTED)
  1843.           send_packet(cpp, DM, FINAL, NULLBUF);
  1844.         cp->reason = LB_DM;
  1845.         setaxstate(cp, DISCONNECTING);
  1846.       }
  1847.       break;
  1848.     case DISC:
  1849.       send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  1850.     case DM:
  1851.     case FRMR:
  1852.       if (cpp && cpp->state == DISCONNECTED)
  1853.         send_packet(cpp, DM, FINAL, NULLBUF);
  1854.       cp->reason = LB_DM;
  1855.       setaxstate(cp, DISCONNECTED);
  1856. #ifdef NETROM
  1857.       nr_derate(cp);    /* TEST */
  1858. #endif
  1859.       break;
  1860.     }
  1861.     break;
  1862.  
  1863.     case CONNECTED:
  1864.       switch (type) {
  1865.     case I:
  1866.     case RR:
  1867.     case RNR:
  1868.     case REJ:
  1869.       stop_timer(&cp->t1);
  1870.       nr = control >> 5;
  1871.       if (((cp->vs - nr) & 7) < cp->unack) {
  1872.         if (!cp->polling) {
  1873.           cp->retries = 0;
  1874.           cp->flushed = 0;
  1875.           if (cp->sndtime[(nr-1)&7]) {
  1876.         int32 rtt = msclock() - cp->sndtime[(nr-1)&7];
  1877.         int32 abserr = rtt > cp->srt ? rtt - cp->srt : cp->srt - rtt;
  1878.         cp->srt = ((AGAIN-1) * cp->srt + rtt + (AGAIN/2)) / AGAIN;
  1879.         cp->mdev = ((DGAIN-1) * cp->mdev + abserr + (DGAIN/2)) >> 3;
  1880.         reset_t1(cp);
  1881.           }
  1882.           if (cp->cwind < cp->iface->flags->maxframe) {
  1883.         cp->mdev += (cp->srt / cp->cwind) >> 3;
  1884.         cp->cwind++;
  1885.           }
  1886.         }
  1887.         while (((cp->vs - nr) & 7) < cp->unack) {
  1888.           cp->rxasm = free_p(cp->rxasm);
  1889.           cp->unack--;
  1890.         }
  1891.         if (cpp && cpp->rnrsent && !busy(cpp))
  1892.           send_ack(cpp, RESP);
  1893.       }
  1894.       if (type == I) {
  1895.         ns = (control >> 1) & 7;
  1896.         pullup(&bp, NULLCHAR, cntrlptr - bp->data + 1 + (cp->mode == STREAM));
  1897.         if (!bp)
  1898.           bp = ambufw(0);
  1899.         if (put_reseq(cp, bp, ns))
  1900.           while ((bp = cp->reseq[cp->vr].bp) != NULLBUF) {
  1901.         cp->reseq[cp->vr].bp = NULLBUF;
  1902.         cp->vr = next_seq(cp->vr);
  1903.         cp->rejsent = 0;
  1904.         processed = 0;
  1905.         if(for_me == 1 && uchar(cntrlptr[1]) != PID_NO_L3) {
  1906.           processed = 1;
  1907.           pid = PULLCHAR(&bp);
  1908.           if(cp->segremain != 0) {
  1909.             /* Reassembly in progress; continue */
  1910.             seg = PULLCHAR(&bp);
  1911.             if(pid == PID_SEGMENT && (seg & SEG_REM) == cp->segremain - 1) {
  1912.               /* Correct, in-order segment */
  1913.               append(&cp->segasm,bp);
  1914.               if((cp->segremain = (seg & SEG_REM)) == 0) {
  1915.             /* Done; kick it upstairs */
  1916.             bp = cp->segasm;
  1917.             cp->segasm = NULLBUF;
  1918.             cp->segremain = 0;
  1919.             pid = PULLCHAR(&bp);
  1920.             switch(pid) {
  1921.               case PID_IP:
  1922.                 ip_route(ifp,ifp,bp,0);
  1923.                 break;
  1924. #ifdef AX25
  1925.               case PID_ARP:
  1926.                 arp_input(ifp,bp);
  1927.                 break;
  1928. #endif
  1929. #ifdef NETROM
  1930.               case PID_NETROM:
  1931.                 nr_route(bp,cp);
  1932.                 break;
  1933. #endif
  1934.             }
  1935.               }
  1936.             } else {
  1937.               /* Error! */
  1938.               free_q(&cp->segasm);
  1939.               cp->segasm = NULLBUF;
  1940.               cp->segremain = 0;
  1941.               free_p(bp);
  1942.             }
  1943.           } else {
  1944.             /* No reassembly in progress */
  1945.             if(pid == PID_SEGMENT) {
  1946.               /* Start reassembly */
  1947.               seg = PULLCHAR(&bp);
  1948.               if(!(seg & SEG_FIRST)) {
  1949.             free_p(bp);    /* not first seg - error! */
  1950.               } else {
  1951.             /* Put first segment on list */
  1952.             cp->segremain = seg & SEG_REM;
  1953.             cp->segasm = bp;
  1954.               }
  1955.             } else {
  1956.               /* Normal frame; send upstairs */
  1957.               switch(pid) {
  1958.             case PID_IP:
  1959.               ip_route(ifp,ifp,bp,0);
  1960.               break;
  1961. #ifdef AX25
  1962.             case PID_ARP:
  1963.               arp_input(ifp,bp);
  1964.               break;
  1965. #endif
  1966. #ifdef NETROM
  1967.             case PID_NETROM:
  1968.               nr_route(bp,cp);
  1969.               break;
  1970. #endif
  1971.               }
  1972.             }
  1973.           }
  1974.         }
  1975.         if(!processed) {
  1976.           if (for_me && cpp == NULLAX25) {
  1977.             cp->rcvcnt += len_p(bp);
  1978.             if(cp->user == 0 || Axi_sock == -1) {    /* */
  1979.               free_p(bp);                         /* */
  1980.             } else {                                /* */
  1981.               if (cp->mode == STREAM)
  1982.             append(&cp->rxq, bp);
  1983.               else
  1984.             enqueue(&cp->rxq, bp);
  1985.             }                                    /* */
  1986.           } else {
  1987.             send_ax25(cpp, bp, cp->mode);
  1988.         }
  1989.           }
  1990.         }
  1991.         if (cmdrsp == POLL) {
  1992.           if (cp->dama)
  1993.         dama_flush(cp);
  1994.           send_ack(cp, FINAL);
  1995.           cp->iface->flags->dama_busy = 1;
  1996.         } else {
  1997.           if(!cp->iface->flags->t2init)
  1998.         send_ack(cp, RESP);
  1999.           else {
  2000.         set_timer(&cp->t2,cp->iface->flags->t2init * 1000L);
  2001.         start_timer(&cp->t2);
  2002.           }
  2003.         }
  2004.         if (cp->r_upcall && cp->rcvcnt)
  2005.           (*cp->r_upcall)(cp, cp->rcvcnt);
  2006.       } else {
  2007.         if (cmdrsp == POLL) {
  2008.           if (cp->dama)
  2009.         dama_flush(cp);
  2010.           send_ack(cp, FINAL);
  2011.           cp->iface->flags->dama_busy = 1;
  2012.         }
  2013.         if (cp->polling && cmdrsp == FINAL)
  2014.           cp->retries = cp->polling = cp->flushed = 0;
  2015.         if (type == RNR) {
  2016.           if (!cp->remotebusy)
  2017.         cp->remotebusy = currtime;
  2018.           set_timer(&cp->t4,cp->iface->flags->t4init * 1000L);
  2019.           start_timer(&cp->t4);
  2020.           cp->cwind = 1;
  2021.         } else {
  2022.           cp->remotebusy = 0;
  2023.           stop_timer(&cp->t4);
  2024.           if (cp->unack && type == REJ) {
  2025.         int  old_vs;
  2026.         struct mbuf *bp1;
  2027.         old_vs = cp->vs;
  2028.         cp->vs = (cp->vs - cp->unack) & 7;
  2029.         cp->sndtime[cp->vs] = 0;
  2030.         dup_p(&bp1, cp->rxasm, 0, MAXINT16);
  2031.         send_packet(cp, I, CMD, bp1);
  2032.         cp->vs = old_vs;
  2033.         cp->cwind = 1;
  2034.           } else if (cp->unack && cmdrsp == FINAL) {
  2035.         struct mbuf *bp1, *qp;
  2036.         cp->vs = (cp->vs - cp->unack) & 7;
  2037.         for (qp = cp->rxasm; qp; qp = qp->anext) {
  2038.           cp->sndtime[cp->vs] = 0;
  2039.           dup_p(&bp1, qp, 0, MAXINT16);
  2040.           send_packet(cp, I, CMD, bp1);
  2041.         }
  2042.           }
  2043.         }
  2044.       }
  2045.       try_send(cp, 1, cp->mode);
  2046.       if (cp->polling || cp->unack && !cp->remotebusy && !cp->dama)
  2047.         start_timer(&cp->t1);
  2048.       if (cp->closed && !cp->txq && !cp->unack
  2049.             || cp->remotebusy && cp->remotebusy + 900l < currtime)
  2050.         setaxstate(cp, DISCONNECTING);
  2051.       break;
  2052.     case SABM:
  2053.       if (cp->dama)
  2054.         cp->iface->flags->dama_busy = 0;
  2055.       send_packet(cp, UA, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  2056.       try_send(cp, 1, cp->mode);
  2057.       if (cp->dama)
  2058.         cp->iface->flags->dama_busy = 1;
  2059.       break;
  2060.     case DISC:
  2061.       if (cp->dama)
  2062.         cp->iface->flags->dama_busy = 0;
  2063.       send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  2064. /*       send_packet(cp, UA, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF); */
  2065.       if (cp->dama)
  2066.         cp->iface->flags->dama_busy = 1;
  2067.     case DM:
  2068.       if (cp->dama && (--ifp->flags->dama_slave) == 0)
  2069.         dama_off(ifp);
  2070.       setaxstate(cp, DISCONNECTED);
  2071.       break;
  2072.     case UA:
  2073.       cp->remotebusy = 0;
  2074.       stop_timer(&cp->t4);
  2075.       if (cp->unack)
  2076.         start_timer(&cp->t1);
  2077.       try_send(cp, 1, cp->mode);
  2078.       break;
  2079.     case FRMR:
  2080.       setaxstate(cp, DISCONNECTING);
  2081.       break;
  2082.     }
  2083.     break;
  2084.  
  2085.     case DISCONNECTING:
  2086.       switch (type) {
  2087.     case I:
  2088.     case RR:
  2089.     case RNR:
  2090.     case REJ:
  2091.     case SABM:
  2092.       if (cp->dama) {
  2093.         cp->iface->flags->dama_busy = 0;
  2094.         send_packet(cp, DISC, POLL, NULLBUF);
  2095.         cp->iface->flags->dama_busy = 1;
  2096.       }
  2097.       else {
  2098.         if (cmdrsp != RESP && cmdrsp != FINAL)
  2099.           send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  2100.       }
  2101.       break;
  2102.     case DISC:
  2103.       if (cp->dama)
  2104.         cp->iface->flags->dama_busy = 0;
  2105.       send_packet(cp, DM, (cmdrsp == POLL) ? FINAL : RESP, NULLBUF);
  2106.       if (cp->dama)
  2107.         cp->iface->flags->dama_busy = 1;
  2108.     case DM:
  2109.     case UA:
  2110.     case FRMR:
  2111.       if (cp->dama && (--ifp->flags->dama_slave) == 0)
  2112.         dama_off(ifp);
  2113.       setaxstate(cp, DISCONNECTED);
  2114.       break;
  2115.     }
  2116.     break;
  2117.     }
  2118.     free_p(bp);
  2119.     return;
  2120. }
  2121.  
  2122. /* set tnc to full duplex if it's our first connection in mode DAMA */
  2123. static void near dama_on(struct iface *ifp) {
  2124.   char *argv[] = {"5","1"};
  2125.  
  2126.   ifp->flags->dama_busy = 1;
  2127.   if (ifp->flags->dama_slave++ || ifp->ioctl == NULLFP)
  2128.     return;
  2129.   (*ifp->ioctl)(ifp,2,argv);
  2130. }
  2131.  
  2132. /* set normal tnc parameter (csma) */
  2133. static void near dama_off(struct iface *ifp) {
  2134.   char *argv[] = {"5","0"};
  2135.  
  2136.     ifp->flags->dama_busy = 0;
  2137.     ifp->flags->dama_slave = 0;
  2138.     if (ifp->ioctl != NULLFP)
  2139.       (*ifp->ioctl)(ifp,2,argv);
  2140. }
  2141.  
  2142. #ifdef AXIP
  2143. static int
  2144. axip_stop(iface)
  2145. struct iface *iface;
  2146. {
  2147.   axipaddr[iface->dev] = 0;
  2148.   return 0;
  2149. }
  2150.  
  2151. /* attach a fake AX.25 interface for AX.25 on top of IP */
  2152. /* argv[0] == "axip"
  2153.  * argv[1] == name of new interface
  2154.  * argv[2] == MTU
  2155.  * argv[3] == hostname of remote end of wormhole
  2156.  * argv[4] == optional callsign is not necessary when using WNOS
  2157.  */
  2158. int
  2159. axip_attach(int argc,char *argv[],void *p) {
  2160.   int i;
  2161.   struct iface *ifp;
  2162.  
  2163.   if(if_lookup(argv[1]) != NULLIF) {
  2164.     tprintf(Ifexist,argv[1]);
  2165.     return -1;
  2166.   }
  2167.   for(i = 0; i < NAX25; ++i)
  2168.     if(axipaddr[i] == 0)
  2169.       break;
  2170.   if(i == NAX25) {
  2171.     tprintf("Max %d AXIP ifaces\n",NAX25);
  2172.     return -1;
  2173.   }
  2174.   if((axipaddr[i] = resolve(argv[3])) == 0) {
  2175.     tprintf(Badhost,argv[3]);
  2176.     return -1;
  2177.   }
  2178.   ifp = (struct iface *)mxallocw(sizeof(struct iface));
  2179.   ifp->dev = i;
  2180.   ifp->addr = Ip_addr;
  2181.   ifp->name = strxdup(argv[1]);
  2182.   ifp->mtu = atoi(argv[2]);
  2183.   setencap(ifp,"AX25");
  2184.   ifp->hwaddr = strxdup(Mycall);
  2185.   ifp->raw = axip_raw;
  2186.   ifp->stop = axip_stop;
  2187.   init_maxheard(ifp);
  2188.   init_flags(ifp);
  2189.   ifp->niface = Niface++;
  2190.   ifp->next = Ifaces;
  2191.   Ifaces = ifp;
  2192.   return 0;
  2193. }
  2194. #endif /* AXIP */
  2195.